/*
 @ Author: James White
 * Last modified- 1/23/10 James
 * 
 */


package fatboy.communication;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class ObjectLoader implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static volatile Map<ObjectID,Object> objDatabase = new HashMap<ObjectID,Object>();
	private final String fileName = "objDatabase.obj";
	
	public ObjectLoader(){
		loadObjects();
		//Preferences prefs = Preferences.systemNodeForPackage(c)
	}
	

	/**
	 * Loads the database from the local filename
	 * 
	 */
	private void loadObjects(){
		objDatabase = new HashMap<ObjectID, Object>();
		try {
			FileInputStream fis = new FileInputStream(fileName);
		    ObjectInputStream ois = new ObjectInputStream(fis);
		    
			objDatabase = ((ObjectLoader) ois.readObject()).objDatabase;
			
			
		    ois.close();
		    fis.close();
		}catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			compileFile();
			loadObjects();
			return;
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	/**
	 * Finds object from map
	 * 
	 * @param objectID
	 * @return Object associated with given ObjectID
	 */
	public Object getObject(ObjectID objectID){
//		System.out.println("ObjectID "+objectID);
//		System.out.println("Keys "+objDatabase.keySet().toString());
		
		if(objectID == null) {
			throw new IllegalArgumentException();
		}
		
		return objDatabase.get(objectID);
	}
	public static Object getObjectStatic(ObjectID objectID){
//		System.out.println("ObjectID "+objectID);
//		System.out.println("Keys "+objDatabase.keySet().toString());
		
		if(objectID == null) {
			throw new IllegalArgumentException();
		}
		
		return objDatabase.get(objectID);
	}
	public static void setObjectStatic(ObjectID id, Object instance){
		objDatabase.put(id, instance);
	
	}
	/**
	 * Adds new Instance to map, object ID and object Instance
	 * 
	 * @param id
	 * @param instance
	 */
	public void setObject(ObjectID id, Object instance){
		objDatabase.put(id, instance);
		compileFile();
	}
	
/***************************************/
	//For creating the file of Objects and ObjectID's
	//
	
	public void clearDatabase(){
		objDatabase.clear();
	}

	/**
	 * Updates map by saving map to given file;
	 */
	public void compileFile() {
		
		//Save the ObjectLoader itself
		try {
			FileOutputStream fos = new FileOutputStream(fileName);
		    ObjectOutputStream oos = new ObjectOutputStream(fos);
		    oos.writeObject(this);
		    oos.flush();
		    //oos.reset();
		    oos.close();
		    fos.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		loadObjects();   
	}


	public static void clearStatic() {
		objDatabase.clear();
		
	}
}
